<template>
  <div>
    <div>
<!--      EditCom {{isEdit}} {{columns}}-->
      <z-form-template
          :slots="control.slots"
          :def="control.def"
          ref="controlForm"
          @fire="control.onFire"
          @ready="onReady"
      >
      </z-form-template>

    </div>
<!--    <el-button type="primary" size="small" @click="sendConfirm">确认</el-button>-->
<!--    <el-button size="small" @click="sendCancel">取消</el-button>-->
  </div>
</template>

<script>
import {CommonCurdDialgMixin} from "@/zpkgs/components/z-datatable/CurdDialgMixin";
import {FormInput} from "@/zpkgs/hooks/form";

export default {
  name: "EditCom",
  mixins: [
    CommonCurdDialgMixin
  ],
  data() {
    let self = this
    let control =  {
      slots: {
        object_beforeend: `
<z-space style="margin-left: 10px;">
<el-button @click="zform__callEvent('edit-form-submit', scope, $event)"">提交</el-button>
<el-button @click="zform__callEvent('edit-cancel', $event)"">取消</el-button>

</z-space>
`
      },
      def: {
        properties: {
          sourceDbName: {
            ...FormInput('源数据库名', {
            }),
            rules: [],
          },
        }
      },
      onFire([type, args, ctx] = []) {
        // console.log(type, args, ctx)
        if (type === 'edit-form-submit') {
          self.sendConfirm()
        } else if (type === 'edit-cancel') {
          self.sendCancel()
        }
      }
    }
    return {
      control
    }
  },

  methods: {
    onReady() {
      if (this.isEdit) {
        // console.log(this.$refs.controlForm.getCurForm())
        let form = this.$refs.controlForm.getCurForm()
        if (form) {
          form.importData(this.row)
        }
      }
    },
    sendConfirm() {
      let form = this.$refs.controlForm.getCurForm()
      let ret = {}
      if (form) {
        ret.data = form.getCloneData()
      }
      if (this.isEdit) {
        ret.id = ret.data[this.columnKey]
      }
      this.$emit('confirm', ret)
    },
    sendCancel() {
      this.$emit('cancel', {
      })
    }
  }
}
</script>
